#                                                                    -*-perl-*-

$description = "Test automatic variable setting.";

$details = "";

use Cwd;

$dir = cwd;
$dir =~ s,.*/([^/]+)$,../$1,;

# simple test
# Touch these into the past
&utouch(-10, qw(foo.x baz.z));

run_make_test(q!
.SUFFIXES:
.SUFFIXES: .x .y .z
$(dir)/foo.x : baz.z $(dir)/bar.y baz.z
#TAB#@echo '$$@ = $@, $$(@D) = $(@D), $$(@F) = $(@F)'
#TAB#@echo '$$* = $*, $$(*D) = $(*D), $$(*F) = $(*F)'
#TAB#@echo '$$< = $<, $$(<D) = $(<D), $$(<F) = $(<F)'
#TAB#@echo '$$^ = $^, $$(^D) = $(^D), $$(^F) = $(^F)'
#TAB#@echo '$$+ = $+, $$(+D) = $(+D), $$(+F) = $(+F)'
#TAB#@echo '$$? = $?, $$(?D) = $(?D), $$(?F) = $(?F)'
#TAB#touch $@

$(dir)/bar.y baz.z : ; touch $@
!,
              "dir=$dir", "touch $dir/bar.y
\$\@ = $dir/foo.x, \$(\@D) = $dir, \$(\@F) = foo.x
\$* = $dir/foo, \$(*D) = $dir, \$(*F) = foo
\$< = baz.z, \$(<D) = ., \$(<F) = baz.z
\$^ = baz.z $dir/bar.y, \$(^D) = . $dir, \$(^F) = baz.z bar.y
\$+ = baz.z $dir/bar.y baz.z, \$(+D) = . $dir ., \$(+F) = baz.z bar.y baz.z
\$? = $dir/bar.y, \$(?D) = $dir, \$(?F) = bar.y
touch $dir/foo.x\n");

unlink(qw(foo.x bar.y baz.z));

# test the SysV emulation of $$@ etc.

run_make_test(q!
.SECONDEXPANSION:
.SUFFIXES:
.DEFAULT: ; @echo '$@'

$(dir)/foo $(dir)/bar: $@.x $$@.x $$$@.x $$$$@.x $$(@D).x $$(@F).x

$(dir)/x.z $(dir)/y.z: $(dir)/%.z : $@.% $$@.% $$$@.% $$$$@.% $$(@D).% $$(@F).%

$(dir)/biz: $$(@).x $${@}.x $${@D}.x $${@F}.x
!,
              "dir=$dir $dir/foo $dir/bar",
              ".x\n$dir/foo.x\nx\n\$@.x\n$dir.x\nfoo.x\n$dir/bar.x\nbar.x\n");

run_make_test(undef, "dir=$dir $dir/x.z $dir/y.z",
              ".x\n$dir/x.z.x\nx\n\$@.x\n$dir.x\nx.z.x\n.y\n$dir/y.z.y\ny\n\$@.y\n$dir.y\ny.z.y\n");

run_make_test(undef, "dir=$dir $dir/biz", "$dir/biz.x\n$dir.x\nbiz.x\n");

# TEST #2 -- test for Savannah bug #12320.
#
run_make_test('
.SUFFIXES: .b .src

mbr.b: mbr.src
#TAB#@echo $*

mbr.src: ; @:',
              '',
              'mbr');

# Same as above with second expansion.
#
run_make_test('
.SECONDEXPANSION:
.SUFFIXES: .b .src

p:=mbr.src
mbr.b: $$p
#TAB#@echo $*

mbr.src: ; @:',
              '',
              'mbr');

# Test that $* is set to empty string for unknown suffixes.

run_make_test('
mbr.b: mbr.src
#TAB#@echo star=$*

mbr.src: ; @:',
              '',
              "star=\n");

# TEST #3 -- test for Savannah bug #8154
# Make sure that nonexistent prerequisites are listed in $?, since they are
# considered reasons for the target to be rebuilt.
#
# See also Savannah bugs #16002 and #16051.

touch('foo');

run_make_test('
foo: bar ; @echo "\$$? = $?"
bar: ;',
              '',
              '$? = bar');

unlink('foo');

# TEST #4: ensure prereq ordering is correct when the command target has none
# See Savannah bug #21198

run_make_test('
all : A B
all : ; @echo $@ -- $^ -- $<
all : C D
all : E F
A B C D E F G H : ; @:
',
              '', "all -- A B C D E F -- A\n");

1;
